package com.itqn.servlet;

import com.itqn.pojo.Category;
import com.itqn.service.CategoryService;
import com.itqn.service.Impl.CategoryServiceImpl;
import lombok.SneakyThrows;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(value = "/CategoryAddServlet")
public class CategoryAddServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @SneakyThrows
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 解决post提交中文乱码
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html; charset=UTF-8");
        // 获取文本框的值
        String name = request.getParameter("CategoryName");
        // 创建实体类对象
        Category category=new Category();
        // 给实体类对象的属性设置值
        category.setName(name);
        // 创建业务逻辑层对象
        CategoryService categoryService=new CategoryServiceImpl();
        // 调用添加方法
        int result = categoryService.AddCategory(category);
        // 判断
        PrintWriter writer = response.getWriter();
        writer.write(result>0?"ok":"no");
    }
}